package ubadbtools.deadlockAnalyzer.gui.forms;

import java.awt.Frame;

import java.util.Iterator;

import java.util.Set;

import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import ubadbtools.deadlockAnalyzer.common.DeadlockModelType;
import ubadbtools.deadlockAnalyzer.common.Schedule;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class AnalyzeScheduleDialog extends JDialog
{
	//[start] Constructor
    public AnalyzeScheduleDialog(Frame parent, boolean modal, Schedule log)
    {
    	super(parent, modal);
        initComponents();
        
        
        // Analizo cada aspecto del log
        //TODO: Completar
        String s,sAux;
        sAux = "";
        Set<String> noSonS2PL;
        Set<String> noSon2PL;
        
        if (!log.isAnyCommited())
        	 s = "No hay transacciones que hayan hecho COMMIT";
        	//	jTextArea1.setText("No hay transacciones que hayan hecho COMMIT");
        else
        {     
        	noSonS2PL = log.isS2PL();
        	if(noSonS2PL.isEmpty()){
       		 s = "La historia es S2PL";
        	} else {
        		noSon2PL = log.is2PL();
           	   
        		if(noSon2PL.isEmpty()){
           		     s = "La historia no es S2PL.\nLas Transacciones que no cumplen con S2PL son:";
           		     for(Iterator<String> i = noSonS2PL.iterator();i.hasNext();){
          		          String s2 = i.next();
          		          sAux = sAux.concat(" ").concat(s2);
          		     }
           		     s = s.concat(sAux).concat(".\n").concat("La historia es 2PL.");
           	     
           		     
           	     } else {
           		     for(Iterator<String> i = noSon2PL.iterator();i.hasNext();){
           		        String s2 = i.next();
           		        sAux = sAux.concat(" ").concat(s2);
           		     }
           		     s = "La historia no es 2PL y por lo tanto no es S2PL. \nLas transacciones que no cumplen con 2PL son: ";
           		     s = s.concat(sAux);
           	    }
        	}      	
        	 
        }
        
        DeadlockModelType deadlockModelType = log.getDeadlockModelType();
        String todo = "";
        switch(deadlockModelType)
        {
                case PREVENTION:
                	todo = s;
                	break;
                case DETECTION:
                	 String estaEnDeadlock;
                     boolean hayCiclos = log.DetectarDeadLock();
                     if(hayCiclos)
                     	estaEnDeadlock = "La historia esta en deadlock.";
                     else
                     	estaEnDeadlock = "La historia no esta en deadlock.";
                   
                   todo = s.concat("\n").concat("\n").concat(estaEnDeadlock);
                    break;
        }
       
        jTextArea1.setText(todo);
        
    }
    //[end]
    
    //[start] showDialog
    public static void showDialog(Frame parent, Schedule log)
    {
    	AnalyzeScheduleDialog dialog = new AnalyzeScheduleDialog(parent, true, log);
        dialog.setVisible(true);
    }
    //[end]
    
    //[start] ShowErrorMessage
	/**
     * Muestra un mensaje de error al usuario
     * @param message
     */
	public void ShowErrorMessage(String message)
	{
		JOptionPane.showConfirmDialog(
			this,
			message,
			"Error",
			JOptionPane.DEFAULT_OPTION,
			JOptionPane.ERROR_MESSAGE);
	}
	//[end]
	
    //[start] InitComponents (AUTO-GENERATED)
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    	GroupLayout layout = new GroupLayout((JComponent)getContentPane());
    	getContentPane().setLayout(layout);
        butCerrar = new javax.swing.JButton();

        this.setTitle("An�lisis de la historia");

        butCerrar.setText("Cerrar");
        {
        	jTextArea1 = new JTextArea();
        	jTextArea1.setText("Ac� deben incluir el an�lisis de 2PL, S2PL y la detecci�n de deadlocks (si fue seleccionada  previamente)");
        	jTextArea1.setLineWrap(true);
        }
        layout.setVerticalGroup(layout.createSequentialGroup()
        	.addContainerGap(36, 36)
        	.addComponent(jTextArea1, GroupLayout.PREFERRED_SIZE, 305, GroupLayout.PREFERRED_SIZE)
        	.addGap(341)
        	.addComponent(butCerrar, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setHorizontalGroup(layout.createSequentialGroup()
        	.addContainerGap(63, 63)
        	.addComponent(jTextArea1, 0, 350, Short.MAX_VALUE)
        	.addGap(23)
        	.addComponent(butCerrar, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addContainerGap());
        butCerrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butCerrarMouseClicked(evt);
            }
        });

        pack();
    }// </editor-fold>//GEN-END:initComponents
	//[end]
    
    //[start] Eventos
    private void butCerrarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butCerrarMouseClicked
        this.setVisible(false);
    }//GEN-LAST:event_butCerrarMouseClicked
	//[end]
	
	//[start] Variables Form (AUTO-GENERATED)
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butCerrar;
    private JTextArea jTextArea1;
    
    // End of variables declaration//GEN-END:variables
    //[end]
}
